package com.navinfo.platform.dataanalysis.dto;

import com.navinfo.platform.common.utils.DataUtil;
import lombok.Data;
import lombok.ToString;
import org.apache.spark.sql.Column;
import org.apache.spark.sql.Row;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;

import static org.apache.spark.sql.functions.col;

@Data
@ToString
public class StatisticsInfoDto implements Serializable {

    private long uid;
    private long tid;
    private int hsbTimes;
    private int hsbDuration;
    private int hsbMileage;
    private int hsb100kmTimes;
    private int hsbDurationProportion;
    private int hsbMileageProportion;
    private int csbTimes;
    private int csbDuration;
    private int csbMileage;
    private int csb100kmTimes;
    private int csbDurationProportion;
    private int csbMileageProportion;
    private int clutchTimes;
    private int clutchDuration;
    private int clutchMileage;
    private int clutch100kmTimes;
    private int clutchDurationProportion;
    private int clutchMileageProportion;
    private int foutTimes;
    private int foutDuration;
    private int fout100kmTimes;
    private int foutDurationProportion;
    private int idleTimes;
    private int idleDuration;
    private int idle100kmTimes;
    private int idleDurationProportion;
    private int ngTimes;
    private int ngDuration;
    private int ngMileage;
    private int ng100kmTimes;
    private int ngDurationProportion;
    private int ngMileageProportion;
    private int so90Times;
    private int so90Duration;
    private int so90Mileage;
    private int so90100kmTimes;
    private int so90DurationProportion;
    private int so90MileageProportion;
    private int so120Times;
    private int so120Duration;
    private int so120Mileage;
    private int so120100kmTimes;
    private int so120DurationProportion;
    private int so120MileageProportion;
    private int srTimes;
    private int srDuration;
    private int srMileage;
    private int sr100kmTimes;
    private int srDurationProportion;
    private int srMileageProportion;
    private int lrogTimes;
    private int lrogDuration;
    private int lrogMileage;
    private int lrog100kmTimes;
    private int lrogDurationProportion;
    private int lrogMileageProportion;
    private int mileage;
    private int workDuration;
    private Timestamp updateTime;
    private String day;

    public static StatisticsInfoDto parseRow(Row row){
        StatisticsInfoDto statisticsInfoDto = new StatisticsInfoDto();
        statisticsInfoDto.setTid(row.getAs("tid"));
        statisticsInfoDto.setUid(statisticsInfoDto.getTid());
        statisticsInfoDto.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        statisticsInfoDto.setDay(row.getAs("day"));
        Integer mileage = row.getAs("mileage");
        mileage = mileage == null ? 0 : mileage;
        statisticsInfoDto.setMileage(mileage);
        Integer workDuration = row.getAs("workDuration");
        workDuration = workDuration == null ? 0 : workDuration;
        statisticsInfoDto.setWorkDuration(workDuration);
        //设置普通刹车统计数据
        statisticsInfoDto.setCsbTimes(row.getAs("csbTimes"));
        statisticsInfoDto.setCsbDuration(row.getAs("csbDuration"));
        statisticsInfoDto.setCsbMileage(row.getAs("csbMileage"));
        statisticsInfoDto.setCsb100kmTimes(DataUtil.hundredKmTimes(statisticsInfoDto.getCsbTimes(), mileage));
        statisticsInfoDto.setCsbDurationProportion(DataUtil.durationProportion(statisticsInfoDto.getCsbDuration(), workDuration));
        statisticsInfoDto.setCsbMileageProportion(DataUtil.mileageProportion(statisticsInfoDto.getCsbMileage(), mileage));
        //设置高速刹车统计数据
        statisticsInfoDto.setHsbTimes(row.getAs("hsbTimes"));
        statisticsInfoDto.setHsbDuration(row.getAs("hsbDuration"));
        statisticsInfoDto.setHsbMileage(row.getAs("hsbMileage"));
        statisticsInfoDto.setHsb100kmTimes(DataUtil.hundredKmTimes(statisticsInfoDto.getHsbTimes(), mileage));
        statisticsInfoDto.setHsbDurationProportion(DataUtil.durationProportion(statisticsInfoDto.getHsbDuration(), workDuration));
        statisticsInfoDto.setHsbMileageProportion(DataUtil.mileageProportion(statisticsInfoDto.getHsbMileage(), mileage));
        //设置离合统计数据
        statisticsInfoDto.setClutchTimes(row.getAs("clutchTimes"));
        statisticsInfoDto.setClutchDuration(row.getAs("clutchDuration"));
        statisticsInfoDto.setClutchMileage(row.getAs("clutchMileage"));
        statisticsInfoDto.setClutch100kmTimes(DataUtil.hundredKmTimes(statisticsInfoDto.getClutchTimes(), mileage));
        statisticsInfoDto.setClutchDurationProportion(DataUtil.durationProportion(statisticsInfoDto.getClutchDuration(), workDuration));
        statisticsInfoDto.setClutchMileageProportion(DataUtil.mileageProportion(statisticsInfoDto.getClutchMileage(), mileage));
        //设置IDLE统计数据
        statisticsInfoDto.setIdleTimes(row.getAs("idleTimes"));
        statisticsInfoDto.setIdleDuration(row.getAs("idleDuration"));
        statisticsInfoDto.setIdle100kmTimes(DataUtil.hundredKmTimes(statisticsInfoDto.getIdleTimes(), mileage));
        statisticsInfoDto.setIdleDurationProportion(DataUtil.durationProportion(statisticsInfoDto.getIdleDuration(), workDuration));
        //设置空挡滑行统计
        statisticsInfoDto.setNgTimes(row.getAs("ngTimes"));
        statisticsInfoDto.setNgDuration(row.getAs("ngDuration"));
        statisticsInfoDto.setNgMileage(row.getAs("ngMileage"));
        statisticsInfoDto.setNg100kmTimes(DataUtil.hundredKmTimes(statisticsInfoDto.getNgTimes(), mileage));
        statisticsInfoDto.setNgDurationProportion(DataUtil.durationProportion(statisticsInfoDto.getNgDuration(), workDuration));
        statisticsInfoDto.setNgMileageProportion(DataUtil.mileageProportion(statisticsInfoDto.getNgMileage(), mileage));
        //设置熄火统计数据
        statisticsInfoDto.setFoutTimes(row.getAs("foutTimes"));
        statisticsInfoDto.setFoutDuration(row.getAs("foutDuration"));
        statisticsInfoDto.setFout100kmTimes(DataUtil.hundredKmTimes(statisticsInfoDto.getFoutTimes(), mileage));
        statisticsInfoDto.setFoutDurationProportion(DataUtil.durationProportion(statisticsInfoDto.getFoutDuration(), workDuration));
        //超速90km数据hs90kmInfoCache
        statisticsInfoDto.setSo90100kmTimes(DataUtil.hundredKmTimes(statisticsInfoDto.getSo90Times(), mileage));
        statisticsInfoDto.setSo90DurationProportion(DataUtil.durationProportion(statisticsInfoDto.getSo90Duration(), workDuration));
        statisticsInfoDto.setSo90MileageProportion(DataUtil.mileageProportion(statisticsInfoDto.getSo90Mileage(), mileage));
        //超速120km数据
        statisticsInfoDto.setSo120100kmTimes(DataUtil.hundredKmTimes(statisticsInfoDto.getSo120Times(), mileage));
        statisticsInfoDto.setSo120DurationProportion(DataUtil.durationProportion(statisticsInfoDto.getSo120Duration(), workDuration));
        statisticsInfoDto.setSo120MileageProportion(DataUtil.mileageProportion(statisticsInfoDto.getSo120Mileage(), mileage));
        //超转速
        statisticsInfoDto.setSr100kmTimes(DataUtil.hundredKmTimes(statisticsInfoDto.getSrTimes(), mileage));
        statisticsInfoDto.setSrDurationProportion(DataUtil.durationProportion(statisticsInfoDto.getSrDuration(), workDuration));
        statisticsInfoDto.setSrMileageProportion(DataUtil.mileageProportion(statisticsInfoDto.getSrMileage(), mileage));
        //低转速脱档
        statisticsInfoDto.setLrog100kmTimes(DataUtil.hundredKmTimes(statisticsInfoDto.getLrogTimes(), mileage));
        statisticsInfoDto.setLrogDurationProportion(DataUtil.durationProportion(statisticsInfoDto.getLrogDuration(), workDuration));
        statisticsInfoDto.setLrogMileageProportion(DataUtil.mileageProportion(statisticsInfoDto.getLrogMileage(), mileage));
        return statisticsInfoDto;
    }

    public static Column[] alarmColumns(){
        Column uid = col("uid").as("uid");
        Column tid = col("tid").as("tid");
        Column so90Times =	col("so90Times").as("so_90_times");
        Column so90Duration =	col("so90Duration").as("so_90_duration");
        Column so90Mileage =	col("so90Mileage").as("so_90_mileage");
        Column so90100kmTimes =	col("so90100kmTimes").as("so_90_100km_times");
        Column so90DurationProportion =	col("so90DurationProportion").as("so_90_duration_proportion");
        Column so90MileageProportion =	col("so90MileageProportion").as("so_90_mileage_proportion");
        Column so120Times =	col("so120Times").as("so_120_times");
        Column so120Duration =	col("so120Duration").as("so_120_duration");
        Column so120Mileage =	col("so120Mileage").as("so_120_mileage");
        Column so120100kmTimes =	col("so120100kmTimes").as("so_120_100km_times");
        Column so120DurationProportion =	col("so120DurationProportion").as("so_120_duration_proportion");
        Column so120MileageProportion =	col("so120MileageProportion").as("so_120_mileage_proportion");
        Column srTimes =	col("srTimes").as("sr_times");
        Column srDuration =	col("srDuration").as("sr_duration");
        Column srMileage =	col("srMileage").as("sr_mileage");
        Column sr100kmTimes =	col("sr100kmTimes").as("sr_100km_times");
        Column srDurationProportion =	col("srDurationProportion").as("sr_duration_proportion");
        Column srMileageProportion =	col("srMileageProportion").as("sr_mileage_proportion");
        Column lrogTimes =	col("lrogTimes").as("lrog_times");
        Column lrogDuration =	col("lrogDuration").as("lrog_duration");
        Column lrogMileage =	col("lrogMileage").as("lrog_mileage");
        Column lrog100kmTimes =	col("lrog100kmTimes").as("lrog_100km_times");
        Column lrogDurationProportion =	col("lrogDurationProportion").as("lrog_duration_proportion");
        Column lrogMileageProportion =	col("lrogMileageProportion").as("lrog_mileage_proportion");
        Column mileage = col("mileage");
        Column duration = col("workDuration").as("duration");
        Column updateTime = col("updateTime").as("update_time");
        Column day = col("day").as("day");
        return new Column[]{uid,tid,so90Times,so90Duration,so90Mileage,so90100kmTimes,so90DurationProportion,so90MileageProportion,so120Times,
                so120Duration,so120Mileage,so120100kmTimes,so120DurationProportion,so120MileageProportion,srTimes,srDuration,srMileage,sr100kmTimes,
                srDurationProportion,srMileageProportion,lrogTimes,lrogDuration,lrogMileage,lrog100kmTimes,lrogDurationProportion,lrogMileageProportion,
                mileage,duration, updateTime,day};
    }

    public static Column[] eventColumns(){
        Column uid = col("uid").as("uid");
        Column tid = col("tid").as("tid");
        Column hsbTimes = col("hsbTimes").as("hsb_times");
        Column hsbDuration = col("hsbDuration").as("hsb_duration");
        Column hsbMileage = col("hsbMileage").as("hsb_mileage");
        Column hsb100kmTimes = col("hsb100kmTimes").as("hsb_100km_times");
        Column hsbDurationProportion = col("hsbDurationProportion").as("hsb_duration_proportion");
        Column hsbMileageProportion = col("hsbMileageProportion").as("hsb_mileage_proportion");
        Column csbTimes = col("csbTimes").as("csb_times");
        Column csbDuration = col("csbDuration").as("csb_duration");
        Column csbMileage = col("csbMileage").as("csb_mileage");
        Column csb100kmTimes = col("csb100kmTimes").as("csb_100km_times");
        Column csbDurationProportion = col("csbDurationProportion").as("csb_duration_proportion");
        Column csbMileageProportion = col("csbMileageProportion").as("csb_mileage_proportion");
        Column clutchTimes = col("clutchTimes").as("clutch_times");
        Column clutchDuration = col("clutchDuration").as("clutch_duration");
        Column clutchMileage = col("clutchMileage").as("clutch_mileage");
        Column clutch100kmTimes = col("clutch100kmTimes").as("clutch_100km_times");
        Column clutchDurationProportion = col("clutchDurationProportion").as("clutch_duration_proportion");
        Column clutchMileageProportion = col("clutchMileageProportion").as("clutch_mileage_proportion");
        Column foutTimes = col("foutTimes").as("fout_times");
        Column foutDuration = col("foutDuration").as("fout_duration");
        Column fout100kmTimes = col("fout100kmTimes").as("fout_100km_times");
        Column foutDurationProportion = col("foutDurationProportion").as("fout_duration_proportion");
        Column idleTimes = col("idleTimes").as("idle_times");
        Column idleDuration = col("idleDuration").as("idle_duration");
        Column idle100kmTimes = col("idle100kmTimes").as("idle_100km_times");
        Column idleDurationProportion = col("idleDurationProportion").as("idle_duration_proportion");
        Column ngTimes = col("ngTimes").as("ng_times");
        Column ngDuration = col("ngDuration").as("ng_duration");
        Column ngMileage = col("ngMileage").as("ng_mileage");
        Column ng100kmTimes = col("ng100kmTimes").as("ng_100km_times");
        Column ngDurationProportion = col("ngDurationProportion").as("ng_duration_proportion");
        Column ngMileageProportion = col("ngMileageProportion").as("ng_mileage_proportion");
        Column mileage = col("mileage").as("mileage");
        Column duration = col("workDuration").as("duration");
        Column updateTime = col("updateTime").as("update_time");
        Column day = col("day").as("day");
        return new Column[]{uid,tid,hsbTimes,hsbDuration,hsbMileage,hsb100kmTimes,hsbDurationProportion,hsbMileageProportion,
                csbTimes,csbDuration,csbMileage,csb100kmTimes,csbDurationProportion,csbMileageProportion,clutchTimes,clutchDuration,
                clutchMileage,clutch100kmTimes,clutchDurationProportion,clutchMileageProportion,foutTimes,foutDuration,fout100kmTimes,
                foutDurationProportion,idleTimes,idleDuration,idle100kmTimes,idleDurationProportion,ngTimes,ngDuration,ngMileage,
                ng100kmTimes,ngDurationProportion,ngMileageProportion,mileage,duration, updateTime,day};
    }
}
